﻿namespace H2.Streams.Sections
{
    using H2.Streams;
    using System;
    using System.Reflection;

    public class Sbsp
    {
        private MapStream Map;
        internal SbspInfo[] SbspInfoCache;

        internal Sbsp(MapStream map)
        {
            this.Map = map;
        }

        internal void CreateCache()
        {
            this.Map.Position = this.Map.Scnr.Offset + 0x210;
            int num = this.Map.ReadInt32();
            int num2 = this.Map.ReadInt32() - this.Map.SecondaryMagic;
            this.SbspInfoCache = new SbspInfo[num];
            for (int i = 0; i < num; i++)
            {
                this.Map.Position = num2 + (i * 0x44);
                this.SbspInfoCache[i] = new SbspInfo(this.Map, (int) this.Map.Position, i);
                TagInfo info = this.Map.Tags[this.SbspInfoCache[i].ID, Tags.SearchType.ID];
                info.OffsetSet = this.SbspInfoCache[i].Offset;
                info.Size = this.SbspInfoCache[i].Size;
                int lightMapID = this.SbspInfoCache[i].LightMapID;
                if (lightMapID != -1)
                {
                    info = this.Map.Tags[lightMapID, Tags.SearchType.ID];
                    info.OffsetSet = this.SbspInfoCache[i].LightMap.Offset;
                    info.Size = this.SbspInfoCache[i].LightMap.Size;
                    this.SbspInfoCache[i].LightMap.StartOffset = this.SbspInfoCache[i].LightMap.Offset;
                }
            }
        }

        public int BSPCount
        {
            get
            {
                return this.Map.ReadInt32At(this.Map.Scnr.Offset + 0x210, false);
            }
            set
            {
                this.Map.WriteAt(this.Map.Scnr.Offset + 0x210, value, false);
            }
        }

        public int BSPTransReflx
        {
            get
            {
                return (this.Map.ReadInt32At(this.Map.Scnr.Offset + 0x214, false) - this.Map.SecondaryMagic);
            }
            set
            {
                this.Map.WriteAt(this.Map.Scnr.Offset + 0x214, (int) (value + this.Map.SecondaryMagic), false);
            }
        }

        public SbspInfo this[string Path]
        {
            get
            {
                foreach (SbspInfo info in this.SbspInfoCache)
                {
                    if (info.TagPath == Path)
                    {
                        return info;
                    }
                }
                return null;
            }
            set
            {
                for (int i = 0; i < this.SbspInfoCache.Length; i++)
                {
                    if (this.SbspInfoCache[i].TagPath == Path)
                    {
                        this.SbspInfoCache[i] = value;
                        return;
                    }
                }
            }
        }

        public SbspInfo this[int i, Tags.SearchType st]
        {
            get
            {
                if (st == Tags.SearchType.Index)
                {
                    return this.SbspInfoCache[i];
                }
                for (int j = 0; j < this.SbspInfoCache.Length; j++)
                {
                    if (this.SbspInfoCache[j].ID == i)
                    {
                        return this.SbspInfoCache[j];
                    }
                }
                return null;
            }
            set
            {
                if (st == Tags.SearchType.Index)
                {
                    this.SbspInfoCache[i] = value;
                }
                else
                {
                    for (int j = 0; j < this.SbspInfoCache.Length; j++)
                    {
                        if (this.SbspInfoCache[j].ID == i)
                        {
                            this.SbspInfoCache[j] = value;
                            return;
                        }
                    }
                }
            }
        }
    }
}

